package com.kylin.common.message;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.kylin.common.io.PropertiesUtils;

/**
 * 阿里云短信服务
 *
 * @author wuhao
 * @version 1.0 - 2019-04-05
 */
public class AliyunMessageApi {

    private static final String ACCESS_KEY_ID = PropertiesUtils.getInstance().getProperty("message.aliyun.ACCESS_KEY_ID");
    private static final String ACCESS_SECRET = PropertiesUtils.getInstance().getProperty("message.aliyun.ACCESS_SECRET");

    /**
     * 发送通知类模版
     * @param TemplateCode 模版消息ID
     * @param TemplateParam 模版参数
     * @param PhoneNumbers 发送号码，多个号码用逗号分隔，最多支持1000个
     * @return 
     * @version 1.0  by wuhao at 2019-04-05
     */
    public static JSONObject sendNoticeMsg(String TemplateCode,JSONObject TemplateParam,String PhoneNumbers){

        JSONObject resultBean = new JSONObject();
        DefaultProfile profile = DefaultProfile.getProfile("default", ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        ///request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
        request.putQueryParameter("SignName", "众友物业");
        request.putQueryParameter("TemplateCode", TemplateCode);
        if(TemplateParam != null) {
            request.putQueryParameter("TemplateParam", TemplateParam.toJSONString());
        }
        request.putQueryParameter("SmsUpExtendCode", "");
        request.putQueryParameter("OutId", "");

        try {
            CommonResponse response = client.getCommonResponse(request);
            resultBean = JSONObject.parseObject(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
            resultBean.put("Code","-99");
            resultBean.put("Message",e.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            resultBean.put("Code","-99");
            resultBean.put("Message",e.toString());
        }
        return resultBean;
    }

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("default", ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "18115880010");
        request.putQueryParameter("SignName", "朝阳国际广场");
        request.putQueryParameter("TemplateCode", "SMS_162735608");
        request.putQueryParameter("TemplateParam", "{\"code\":\"123456\"}");
        request.putQueryParameter("SmsUpExtendCode", "");
        request.putQueryParameter("OutId", "");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
